//
//  LBMStatusToolBar.swift
//  微博
//
//  Created by 刘保满 on 16/5/25.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit

class LBMStatusToolBar: UIView {
    
    
    //添加视图模型属性
    var statusViewModel: LBMStatusViewModel? {
        
        didSet {
            
            repostBtn.setTitle(statusViewModel?.reponstsText, forState: .Normal)
            commentBtn.setTitle(statusViewModel?.commentsText, forState: .Normal)
            goodBtn.setTitle(statusViewModel?.attitudesText, forState: .Normal)
 
            
        }

    }

    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = UIColor.redColor()
        setupUI()
        
    }
    
    //默认报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI界面
    private func setupUI() {
        
        //添加子控件
        addChildBtn(repostBtn, title: "转发", imageName: "timeline_icon_retweet")
        addChildBtn(commentBtn, title: "评论", imageName: "timeline_icon_comment")
        addChildBtn(goodBtn, title: "赞", imageName: "timeline_icon_like")
        
        //添加分隔线
        let sepView1 = lineView()
        let sepView2 = lineView()
        addSubview(sepView1)
        addSubview(sepView2)
        
        
        //设置按钮的约束
        repostBtn.snp_makeConstraints { (make) in
            
            make.top.left.bottom.equalTo(self)
            
        }
        
        commentBtn.snp_makeConstraints { (make) in
            
            make.left.equalTo(repostBtn.snp_right)
            make.right.equalTo(goodBtn.snp_left)
            
            make.top.bottom.equalTo(repostBtn)
            
            make.width.equalTo(repostBtn.snp_width)
            
        }
        
        goodBtn.snp_makeConstraints { (make) in
            
            make.right.equalTo(self.snp_right)
            
            make.top.bottom.equalTo(repostBtn)
            make.width.equalTo(repostBtn.snp_width)
        }
        
        let width = 1 / UIScreen.mainScreen().scale
        //设置分割线的约束
        sepView1.snp_makeConstraints { (make) in
            
            make.left.equalTo(commentBtn.snp_left)
            make.centerY.equalTo(self.snp_centerY)
            make.height.equalTo(self.snp_height).multipliedBy(0.4)
            make.width.equalTo(width)
            
        }
        
        
        sepView2.snp_makeConstraints { (make) in
            
            make.left.equalTo(commentBtn.snp_right)
            make.centerY.equalTo(self.snp_centerY)
            make.height.equalTo(self.snp_height).multipliedBy(0.4)
            make.width.equalTo(width)
            
        }
        
        
    }
    
    //封装一个公用的方法设置按钮
    private func addChildBtn(btn: UIButton, title: String, imageName: String) {
        
        btn.setTitle(title, forState: .Normal)
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(10.0)
        
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        
        btn.sizeToFit()
        
        self.addSubview(btn)
        
        
    }
    
    //按钮之间的分隔线
    private func lineView() -> UIView {
        
        let li = UIView()
        li.backgroundColor = UIColor.purpleColor()
        return li
    }
    
    
    //懒加载子控件
    
    //转发
    private lazy var repostBtn: UIButton = UIButton()
    //评论
    private lazy var commentBtn: UIButton = UIButton()
    //赞
    private lazy var goodBtn: UIButton = UIButton()
    
    
}
